package com.winz.cultureapp.activity;

import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.mine.xrecyclerview.XRecyclerView;
import com.mine.xrecyclerview.rcvadapter.OnItemClickListener;
import com.winz.cultureapp.R;
import com.winz.cultureapp.adapter.xrcv.ExhibitionAdapter;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.ExhibitionListBean;
import com.winz.cultureapp.filter.activity.LocationPopupWindow;
import com.winz.cultureapp.filter.stadium.XxPopupWindow;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.RequestFactory;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.filterbar.FilterBar;
import com.winz.filterbar.FilterTab;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 文化场馆
 */
public class ExhibitionActivity extends BaseAppCompatActivity implements XRecyclerView.LoadingListener, OnItemClickListener, LocationPopupWindow.OnFinishListener, FilterBar.SelectedMenuListener, XxPopupWindow.OnFinishListener {
    @BindView(R.id.tv_top_title)
    public TextView mTv_top_title;
    @BindView(R.id.xrcv_exhibition)
    public XRecyclerView mXrcv_exhibition;
    @BindView(R.id.fb_exhibition)
    public FilterBar mFb_exhibition;

    private boolean isRefresh = true;
    private int limit = 10;
    private int page = 1;
    private int totalPage = 0;

    private String areaId = "0"; // 区域ID
    private String categoryId = "0"; // 分类ID

    private List<ExhibitionListBean.DataBean.ListBean> mDatas;
    private ExhibitionAdapter mAdapter;

    private LocationPopupWindow mPop_location;
    private XxPopupWindow mPop_xx;
    private Map<FilterTab,PopupWindow> map_pop = new HashMap<>();
    @Override
    protected int contentView() {
        return R.layout.activity_exhibition;
    }

    @Override
    protected void initViews() {
        mTv_top_title.setText(getString(R.string.exhibition_top_title));
        mDatas = new ArrayList<>();
        // XrecyclerView
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mXrcv_exhibition.setLayoutManager(manager);
        mXrcv_exhibition.setPullRefreshEnabled(true);
        mXrcv_exhibition.setLoadingMoreEnabled(false);
        mAdapter = new ExhibitionAdapter(this,mDatas,R.layout.empty_view,R.layout.item_exhibition_list);

        // 初始化
        mPop_location = new LocationPopupWindow(this,mFb_exhibition.getTabs().get(0));
        map_pop.put(mFb_exhibition.getTabs().get(0),mPop_location);
        mPop_xx = new XxPopupWindow(this,mFb_exhibition.getTabs().get(1));
        map_pop.put(mFb_exhibition.getTabs().get(1), mPop_xx);
    }

    @Override
    protected void initEvents() {

        mXrcv_exhibition.setAdapter(mAdapter);
        mXrcv_exhibition.setLoadingListener(this);
        mAdapter.setOnItemClickListener(this);

        mFb_exhibition.setSelectedMenuListener(this);

        mPop_location.setOnFinishListener(new LocationPopupWindow.OnFinishListener() {
            @Override
            public void onFinish(FilterTab flag, String data) {
                flag.setText(data.split(",")[0]);
                map_pop.get(flag).dismiss();
                if (data.split(",").length>1){
                    areaId = data.split(",")[1];
                    getList();
                }
            }
        });
        mPop_location.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
//                mFb_activity.
                mPop_location.getFlag().setFilterTabSelected(false,FilterTab.FLAG_OTHER);
            }
        });
        mPop_xx.setOnFinishListener(new XxPopupWindow.OnFinishListener() {
            @Override
            public void onFinish(FilterTab flag, String data) {
                flag.setText(data.split(",")[0]);
                map_pop.get(flag).dismiss();
                if (data.split(",").length>1){
                    categoryId = data.split(",")[1];
                    getList();
                }
            }
        });
        mPop_xx.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
//                mFb_activity.getTabSelectedObervable();
                mPop_xx.getFlag().setFilterTabSelected(false,FilterTab.FLAG_OTHER);
            }
        });
    }

    @Override
    protected void loadDatas(Intent intent) {
        getList();
    }

    /**
     * 获取数据列表
     */
    private void getList() {
        showProgressDialog();

        HttpUitls.onGet("unit_culture", RequestFactory.getInstance().unit_culture(page+"",limit+"",areaId,categoryId), new OnResponeListener<ExhibitionListBean>() {
            @Override
            public void onSuccess(ExhibitionListBean activityListBean) {
                Logger.d("unit_culture==>>", JsonUtil.getJsonFromObj(activityListBean));
                if (activityListBean.code == 0) {
                    totalPage = activityListBean.totalPage;
                    if (activityListBean.data.list != null && activityListBean.data.list.size() > 0) {
                        mXrcv_exhibition.setLoadingMoreEnabled(true);
                        if (isRefresh) {
                            mDatas.clear();
                            mDatas = activityListBean.data.list;
                        } else {
                            mDatas.addAll(activityListBean.data.list);
                        }
                    } else {
                        mDatas.clear();
                        ExhibitionListBean.DataBean.ListBean empty = new ExhibitionListBean.DataBean.ListBean();
                        empty.layout_type = -1;
                        mDatas.add(empty);
                        mXrcv_exhibition.setLoadingMoreEnabled(false);
                    }
                    mAdapter.setDatas(mDatas);
                } else {
                    showMsg(activityListBean.msg);
                }
                if (isRefresh) {
                    mXrcv_exhibition.refreshComplete();
                } else {
                    mXrcv_exhibition.loadMoreComplete();
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                Logger.d("unit_culture==>>", error);
                dismissProgressDialog();
                showMsg(error);
                if (isRefresh) {
                    mXrcv_exhibition.refreshComplete();
                } else {
                    mXrcv_exhibition.loadMoreComplete();
                }
            }
        });
    }


    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 1;
        loadDatas(null);
    }

    /**
     * 加载
     */
    @Override
    public void onLoadMore() {
        isRefresh = false;
        if (page>=totalPage){
            mXrcv_exhibition.setNoMore(true,true,"没有更多数据了");
        }else {
            page++;
            loadDatas(null);
        }
    }

    @Override
    public void onItemClick(ViewGroup parent, View view, int position) {
        Bundle bundle = new Bundle();
        bundle.putString(Constants.KEY.NEWS.ID,mDatas.get(position).unitId);
        bundle.putString(Constants.KEY.NEWS.TOP_TITLE,mDatas.get(position).unitName);
        bundle.putString(Constants.KEY.NEWS.HREF,Constants.URL.IP + "/museum/detail/" + mDatas.get(position).unitId);
        startActivity(ExhibitionDesActivity.class,false,bundle);

    }

    @Override
    public boolean onItemLongClick(ViewGroup parent, View view, int position) {
        return false;
    }

    @Override
    public void onFinish(FilterTab flag, String data) {
        flag.setText(data);
        map_pop.get(flag).dismiss();
        showMsg(data);
    }

    /**
     * 选择筛选项
     */
    @Override
    public void onSelectedMenu(FilterTab view, boolean selected) {
        // 打开或者关闭筛选弹窗
        if (selected){
            map_pop.get(view).showAsDropDown(view);
        }else {
            map_pop.get(view).dismiss();
        }

    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    @OnClick(R.id.fl_back)
    public void onClick(){
        finish();
    }

}
